//
//  AppDelegate.m
//  SinaWeiBo
//
//  Created by WGP on 16/4/9.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "AppDelegate.h"
#import "HMMainTabBarController.h"
#import "HMWelcomeViewController.h"
#import "HMNewFeatureCollectionVC.h"
#import "HMUserAccountViewModel.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
//    self.window.rootViewController = [HMMainTabBarController mainTabBarController];
    
//    欢迎界面调试
//    self.window.rootViewController = [[HMWelcomeViewController alloc] init];
    
//    新特性界面
    self.window.rootViewController = [self didFinishLanuchShouldViewController];
    
    [self.window makeKeyAndVisible];
    
    return YES;
}


-(BOOL)isNewVersion
{
//     1.获得当前版本
    NSString *newVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];
    
//    2.获得上一个版本
    NSString *sandboxVersionKey = @"sandboxVersionKey";
    NSString *sandboxVersion = [[NSUserDefaults standardUserDefaults] stringForKey:sandboxVersionKey];
   
    
//    3.对比
    if(newVersion == sandboxVersion)
    {
        return NO;
    }
    
//    4.保存新版本
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:newVersion forKey:sandboxVersionKey];

    return YES;
}

-(UIViewController *)didFinishLanuchShouldViewController
{
    BOOL userLogin = [HMUserAccountViewModel sharedInstance].userLogin;
    BOOL isNewVersion = [self isNewVersion];
    if(!userLogin)
    {
        return [HMMainTabBarController mainTabBarController];
    }
    
    if (isNewVersion) {
        return [HMNewFeatureCollectionVC newFeatureCollectionVC];
    }
    return [[HMWelcomeViewController alloc] init];
}

//切换控制器的方法
-(void)switchRootViewController:(UIViewController *)viewController
{
    self.window.rootViewController = viewController;
}

@end
















